
package petrivis.view;

import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import petrivis.model.PetriNet;
import prefuse.render.ShapeRenderer;
import prefuse.visual.DecoratorItem;
import prefuse.visual.VisualItem;

/**
 * Renderer for the tokens
 * @author Jorge Munoz
 */
public class TokenRenderer extends ShapeRenderer{

    private int tokenID;

     /**
     * Creates a new TokenRenderer with default base size of 10 pixels.
     */
    public TokenRenderer(int id) {
        super();
        tokenID = id;
    }

    /**
     * Creates a new TokenRenderer with given base size.
     * @param size the base size in pixels
     */
    public TokenRenderer(int id, int size) {
       super(size);
       tokenID = id;
    }


    @Override
    protected Shape getRawShape(VisualItem item) {
        double x = 0;
        double y = 0;
        VisualItem itemD = ((DecoratorItem)item).getDecoratedItem();
        double width = super.getBaseSize()*itemD.getSize();


        // Calculate the position of the Shape
        if ( width > 1 ) {
            x = calculateX(itemD);
            y = calculateY(itemD);
            System.out.println("X: "+x);
            System.out.println("Y: "+y);
        }

        return super.ellipse(x, y, width, width);
    }

    private double calculateX(VisualItem item){
        double x = 0;
        Rectangle2D bounds = item.getBounds();
        int marking = (Integer) item.get(PetriNet.MAR_COL);

        switch ( marking ) {
        case 0:
            break;
        case 1:
            if(tokenID == 1) x = bounds.getCenterX();
            break;
        case 2:
            if(tokenID == 1) x = (bounds.getWidth()/3)*1+bounds.getMinX();
            else if(tokenID == 2) x = (bounds.getWidth()/3)*2+bounds.getMinX();
            break;
        case 3:
            if(tokenID == 1) x = (bounds.getWidth()/3)*1+bounds.getMinX();
            else if(tokenID == 2) x = (bounds.getWidth()/3)*2+bounds.getMinX();
            else if(tokenID == 3) x = bounds.getCenterX();
            break;
        default:
            if(tokenID == 1) x = (bounds.getWidth()/3)*1+bounds.getMinX();
            else if(tokenID == 2) x = (bounds.getWidth()/3)*2+bounds.getMinX();
            else if(tokenID == 3) x = (bounds.getWidth()/3)*1+bounds.getMinX();
            else if(tokenID == 4) x = (bounds.getWidth()/3)*2+bounds.getMinX();
            break;
        }

        return x;
    }

    private double calculateY(VisualItem item){
        double y = 0;
        Rectangle2D bounds = item.getBounds();
        int marking = (Integer) item.get(PetriNet.MAR_COL);

        switch ( marking ) {
        case 0:
            break;
        case 1:
            if(tokenID == 1) y = bounds.getCenterY();
            break;
        case 2:
            if(tokenID == 1) y = bounds.getCenterY();
            else if(tokenID == 2) y = bounds.getCenterY();
            break;
        case 3:
            if(tokenID == 1) y = (bounds.getHeight()/3)*1+bounds.getMinY();
            else if(tokenID == 2) y = (bounds.getHeight()/3)*1+bounds.getMinY();
            else if(tokenID == 3) y = (bounds.getHeight()/3)*2+bounds.getMinY();
            break;
        default:
            if(tokenID == 1) y = (bounds.getHeight()/3)*1+bounds.getMinY();
            else if(tokenID == 2) y = (bounds.getHeight()/3)*1+bounds.getMinY();
            else if(tokenID == 3) y = (bounds.getHeight()/3)*2+bounds.getMinY();
            else if(tokenID == 4) y = (bounds.getHeight()/3)*2+bounds.getMinY();
            break;
        }

        return y;
    }

}
